var createError = require('http-errors')
var express = require('express')
var path = require('path')
var cookieParser = require('cookie-parser')
var logger = require('morgan')
var session = require("express-session")

var indexRouter = require('./routes/index')
var sessionRouter = require('./routes/session')
var uploadRouter = require('./routes/upload')
var menusRouter = require('./routes/menus')
var permissionsRouter = require('./routes/permissions')
var rolesRouter = require('./routes/roles')
var usersRouter = require('./routes/users')
var deptsRouter = require('./routes/depts')
var dicsRouter = require('./routes/dics')
var orderRouter = require('./routes/order')
var deviceRouter = require('./routes/devices')
var logsRouter = require('./routes/logs')
var alertsRouter = require('./routes/alerts')
var messagesRouter = require('./routes/messages')
var multer = require('multer');
var app = express()

// view engine setup
app.set('views', path.join(__dirname, 'views'))
app.set('view engine', 'ejs')

// 设置跨域访问，要发在所有路由的最前面
app.all('*', (req, res, next) => {
    res.header('Access-Control-Allow-Origin', '*')
    res.header('Access-Control-Allow-Headers', 'XOrigin, X-Requested-With, Content-Type, Accept, Authorization')
    res.header('Access-Control-Allow-Methods', 'PUT,POST,GET,DELETE,OPTIONS')
    res.header('X-Powered-By', ' 3.2.1')
    // res.header('Content-Type', 'application/jsoncharset=utf-8')
    // next()
    if (req.method === 'OPTIONS') {
        res.status(200).end();
    } else {
        next();
    }
})

app.use(logger('dev'))
app.use(express.json())
app.use(express.urlencoded({
    extended: false
}))
app.use(cookieParser())
app.use(express.static(path.join(__dirname, 'public')))
// 下载
app.use(multer({ dest: './download' }).any());
app.use(express.static("./download"));

// session中间件
app.use(session({
    secret: 'DXOCR',            // session签名
    // name:'session_id',       // 保存在本地cookie的一个名字，默认为connect.sid，可以不设置
    resave: false,              // 强制保存session即使它并没有变化，默认为true，建议设置成false
    saveUninitialized: false,   // 强制将未初始化的session存储，如果设置成true，用户不论是否登录，只要访问网站都会生成一个session，只不过这个session的值是一个空对象
    cookie: {
        maxAge: 30 * 60 * 1000  // 超时时间
    }
}))

app.use('/', indexRouter)
app.use('/session', sessionRouter)
app.use('/upload', uploadRouter)
app.use('/menus', menusRouter)
app.use('/permissions', permissionsRouter)
app.use('/roles', rolesRouter)
app.use('/users', usersRouter)
app.use('/depts', deptsRouter)
app.use('/dics', dicsRouter)
app.use('/order', orderRouter)
app.use('/devices', deviceRouter)
app.use('/logs', logsRouter)
app.use('/alerts', alertsRouter)
app.use('/messages', messagesRouter)



// catch 404 and forward to error handler
app.use(function (req, res, next) {
    next(createError(404))
})

// error handler
app.use(function (err, req, res, next) {
    // set locals, only providing error in development
    res.locals.message = err.message
    res.locals.error = req.app.get('env') === 'development' ? err : {}

    // render the error page
    res.status(err.status || 500)
    res.render('error')
})


module.exports = app
